package com.dingreading.cloud.subject.service.impl;

import com.dingreading.cloud.subject.dto.SubjectsFileDto;
import com.dingreading.cloud.subject.entity.SubjectsFiles;
import com.dingreading.cloud.subject.entity.table.SubjectsFilesTableDef;
import com.dingreading.cloud.subject.mapper.SubjectsFilesMapper;
import com.dingreading.cloud.subject.service.SubjectsFilesService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 备课系统文件管理 服务层实现。
 *
 * @author EDY
 * @since 2025-10-22
 */
@Service
public class SubjectsFilesServiceImpl extends BaseServiceImpl<SubjectsFilesMapper, SubjectsFiles, SubjectsFilesTableDef> implements SubjectsFilesService {

    @Override
    protected SubjectsFilesTableDef getTable() {
        return SubjectsFilesTableDef.subjectsFiles;
    }

    @Override
    public List<SubjectsFiles> getFilesBySubjectId(Long subjectId) {
        QueryCondition condition = table.subjectsId.eq(subjectId)
                .and(table.enabled.eq(1));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public List<SubjectsFileDto> dtoListBySubjectId(Long subjectId) {
        QueryCondition condition = table.subjectsId.eq(subjectId)
                .and(table.enabled.eq(1));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.showOrder.asc());
        return getMapper().selectListWithRelationsByQueryAs(wrapper, SubjectsFileDto.class);
    }

    @Override
    public SubjectsFileDto dtoBySubjectId(Long id) {
        QueryCondition condition = table.id.eq(id)
                .and(table.enabled.eq(1));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        return getMapper().selectOneWithRelationsByQueryAs(wrapper, SubjectsFileDto.class);
    }

}
